-- CommunicatingTipsM
-- Create by zouyb
-- 加载界面中小贴士管理

module("CommunicatingTipsM", package.seeall);

-- 类型
-- 通用
TIP_TYPE_LOGIN           = "login"

-- 迷宫外
TIP_TYPE_ENTER_DUNGOEN  = "enter_dungeon";

-- 迷宫内
TIP_TYPE_ENTER_SKYCITY = "enter_skycity";

-- tips列表
local tipList = {};

-- 当前tip
local curTipList = {};

-- 载入配置表
local _init = false;
local loadCsv = function()
    if _init then return end
    _init = true;

    tipList = {};

    -- 转换下信息表
    local communicating_tips_CSV;

    communicating_tips_CSV = get_communicating_tips_CSV();


    for i = 1, #communicating_tips_CSV do
        local tipType = communicating_tips_CSV[i].type;

        if tipList[tipType] == nil then
            tipList[tipType] = {};
        end

        table.insert(tipList[tipType], communicating_tips_CSV[i]);
    end

    communicating_tips_CSV = {};
end

-- 初始化
function init()
    loadCsv();
end

-- 获取贴士
function getTipsByType(tipType)
    local tips = {};
    if type(tipList[tipType]) == "table" then
        tips = table.deepcopy(tipList[tipType]);

    end
    return tips;
end

-- 显示当前类型的
function isShowCurTypeTip(tipType, startDungeon)

    if tipType == nil then
        return false;
    end

    if tipType == "enter_dungeon"  then
        local restart = ME.user.dbase:queryTemp("restart", 0);
        if restart ~= 1 and not startDungeon then
            return false;
        end
    end

    if type(tipList[tipType]) == "table" then
        return true;
    end

    return false;
end

-- 重置显示list
function resetCurTipList(tipType)
    curTipList = {};
    local tipList = getTipsByType(tipType);

    local index = 1;
    for k, v in pairs(tipList) do
        curTipList[index] = {["id"] = v.id, ["weight"] = v.weight, ["const_weigt"] = v.weight, ["tip"] = v.tip};
        index = index + 1;
    end
end

function resetWeight()
    local sum = 0;
    for k, v in pairs(curTipList) do
        v.weight = v.const_weigt;
        sum = sum + v.const_weigt;
    end

    return sum;
end

-- 获取tip
function getTip()
    if true then
        return getCsvLocStr("communicating_tips_8_1");
    end
    if #table.keys(curTipList) < 1 then
        return getLocStr("communicate_with_server");
    end

    local id;

    -- 设置当前随机范围
    local sum = 0;
    for index = 1, #curTipList do
        sum = sum + curTipList[index].weight;
    end

    if sum < 1 then
        sum = resetWeight();
    end

    -- 获取随机的tip
    local ran = math.random(1, sum);

    sum = 0;

    for index = 1, #curTipList do
        local value = curTipList[index];
        sum = sum + value.weight;
        if value.weight > 0 and ran <= sum then
            -- weight置为0，保证下次抽不到
            value.weight = 0;
            return value.tip;
        end
    end

    -- 返回默认值
    return getLocStr("communicate_with_server");
end